﻿#region --- New BSD License ---
/*
Copyright (c) 2009, Timko Technologies, L.L.C. (http://www.timkotech.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this 
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation 
and/or other materials provided with the distribution.

* Neither the name of Timko Technologies, L.L.C. nor the names of its contributors 
may be used to endorse or promote products derived from this software without 
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace InstantIRC
{
    public class IRCResponse
    {
        /*
         * Note: this class was designed for reading in Quits, Parts, Privmsgs, etc.
         * from the byte stream. Whether or not IRCResponse was the correct name,
         * I am not sure, but it will work for now! ~Charles Timko
         */

        private IRCCmd _cmd; // The command responded by the server
        private long _code = -1; // The server code for the data being sent
        private string _msg; // The response message
        private bool _valid; // If the patter is valid
        private string _target; // The target of the command
        private string _subTarget; // The target of the target
        private string _nick; // The nick of the user who sent the message

        public string Message
        {
            get
            {
                return _msg;
            }
        }

        public long Code
        {
            get
            {
                return _code;
            }
        }

        public IRCCmd Command
        {
            get
            {
                return _cmd;
            }
        }

        public bool IsValid
        {
            get
            {
                return _valid;
            }
        }

        public string Target
        {
            get
            {
                return _target;
            }
        }

        public string SubTarget
        {
            get
            {
                return _subTarget;
            }
        }

        public string Nick
        {
            get
            {
                return _nick;
            }
        }

        /// <summary>
        /// TODO: IRCResponse Needs Summary
        /// </summary>
        /// <param name="input">String to check against RegEx pattern</param>
        public IRCResponse(string input)
        {
            // Note on the pattern: '\n' is due to how we received the bytes from the server
            Regex pattern =
                new Regex(@"(:([\S.]+)![a-zA-Z0-9_]+@[-a-zA-Z0-9_.]+)? ?([A-Z0-9]+) ?(#?[^:][\S.]+)? ?([^:][\S.]+)? :?(.*)");

            Match m = pattern.Match(input);
            _valid = m.Success;
            if (_valid)
            {
                _nick = m.Groups[2].Value;
                if (!long.TryParse(m.Groups[3].Value, out _code))
                    _cmd = (IRCCmd)Enum.Parse(typeof(IRCCmd), m.Groups[3].Value);
                _target = m.Groups[4].Value;
                _subTarget = m.Groups[5].Value;
                _msg = m.Groups[6].Value;
            }
        }
    }
}
